/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.repository;

import fr.adama.dto.AdherentDTO;
import fr.adama.entite.Adherent;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.BooleanJunction;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 *
 * @author mini-john
 */
@Repository
public class AdherentReposiroty extends CrudRepository {

    private static final Logger logger = LoggerFactory.getLogger(AdherentReposiroty.class);

    public List<Adherent> searchByField(AdherentDTO adherent) {
        List<Adherent> searchResults = new ArrayList<>();
        if (adherent == null) {
            return searchResults;
        }
        if (adherent.getNomPrenom() == null && adherent.getNumUrssaf() == null && adherent.getNumSiret() == null) {
            return searchResults;
        }
        boolean onElement = false;
        FullTextSession fSession = Search.getFullTextSession(sessionFactory.getCurrentSession());
        QueryBuilder queryBuilder = fSession.getSearchFactory().buildQueryBuilder().forEntity(Adherent.class).get();

        BooleanJunction<BooleanJunction> queryBool = queryBuilder.bool();
        if (adherent.getNomPrenom() != null && !adherent.getNomPrenom().isEmpty()) {
            onElement = true;
            queryBool = queryBool.should(queryBuilder.keyword().wildcard().onField("nomPrenom").ignoreAnalyzer().matching("*"+adherent.getNomPrenom()+"*").createQuery());
        }
        if (adherent.getNumUrssaf() != null && !adherent.getNumUrssaf().isEmpty()) {
            onElement = true;
            queryBool = queryBool.should(queryBuilder.keyword().wildcard().onField("numUrssaf").ignoreAnalyzer().matching("*"+adherent.getNumUrssaf()+"*").createQuery());
        }
        if (adherent.getNumSiret() != null && !adherent.getNumSiret().isEmpty()) {
            onElement = true;
            queryBool = queryBool.should(queryBuilder.keyword().wildcard().onField("numSiret").ignoreAnalyzer().matching("*"+adherent.getNumSiret()+"*").createQuery());
        }
       
        if (!onElement) {
            return searchResults;
        }
        org.apache.lucene.search.Query luceneQuery = queryBool.createQuery();
        org.hibernate.Query hibernateQuery = fSession.createFullTextQuery(luceneQuery, Adherent.class);
        searchResults = hibernateQuery.list();

        return searchResults;

    }

    

    public Adherent getByNomPrenom(String nomPrenom) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Adherent.class)
                .add(Restrictions.like("nomPrenom", nomPrenom, MatchMode.START));

        return (Adherent) criteria.uniqueResult();
    }

}
